<script setup>
import { useMemberStore } from '@/stores'
import { initWebSocket, closeWebSocket, getBalanceSocket } from '@/utils/websocket_balance'
import { watch } from 'vue'
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
const authStore = useMemberStore()
// 监听store中的token变化,退出登录时token为空，关闭websocket
// 登录时设置token后，初始化websocket
watch(
  () => authStore.profile?.token,
  (newVal) => {
    if (newVal) {
      initWebSocket()
    } else {
      closeWebSocket()
    }
  },
)
// app启动时，查看是否有token，有则初始化websocket
onLaunch(() => {
  console.log('App Launch')
  console.log(authStore.profile?.token)
  // 初始化用户信息
  if (authStore.getToekn) {
    initWebSocket()
  } else {
    return
  }
})
onShow(() => {
  if (getBalanceSocket() && !authStore.getToekn()) {
    console.log('onshow已连接，')
    return
  } else if (!getBalanceSocket() && authStore.getToekn()) {
    console.log('onshow未连接，重连')
    initWebSocket()
  }
})
</script>

<style lang="scss">
// 字体图标
@import '@/styles/fonts.scss';

view,
navigator,
input,
scroll-view {
  box-sizing: border-box;
}

button::after {
  border: none;
}

swiper,
scroll-view {
  flex: 1;
  height: 100%;
  overflow: hidden;
}

image {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}

// 两行省略
.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
